<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 转发端口列管理
 *
 * @icon fa fa-circle-o
 */
class Jdklog extends Backend
{

    /**
     * Jdklog模型对象
     * @var \app\admin\model\Jdklog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Jdklog;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        $param1 = isset($_GET['ids']) ? $_GET['ids'] : 'value';
        $wherejid=[];
        if (isset($_GET['ids'])){
            $jid=Db::table('fa_jdk')->where('id',$_GET['ids'])->find();
            $wherejid['jid'] = $jid['jid'];
            $this->view->assign("ids", $_GET['ids']);

        }else{
            $this->error(__('url缺少参数，重新点击左侧《转发端口》进入，然后新增', ''));
        }

//        halt($wherejid );
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $adminid=  $this->auth->getRuleIdsaaa();
            $admin=Db::table('fa_admin')->where('id',$adminid)->find();
            $list = $this->model
                ->where($wherejid)
                ->where('username',$admin['username'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id','jid','name','dkks','zfip']);
                
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }




    /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function add()
    {

        $param1 = isset($_GET['ids']) ? $_GET['ids'] : 'value';
        $wherejid=[];
//        halt($_GET['ids']);
        if (isset($_GET['ids'])){
            $jid=Db::table('fa_jdk')->where('id',$_GET['ids'])->find();
            $wherejid['jid'] = $jid['jid'];
        }else{
            $this->error(__('url缺少参数，重新点击左侧《转发端口》进入，然后新增', ''));
        }

        if (false === $this->request->isPost()) {
            $this->view->assign("ids", $_GET['ids']);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        $params['jid']=$jid['jid'];
        $params['jdkid']=$jid['id'];
        $params['name']=$jid['name'];

        $adminid=  $this->auth->getRuleIdsaaa();
        $admin=Db::table('fa_admin')->where('id',$adminid)->find();
        $params['username']= $admin['username'];
//        halt($params['jid']);
        // table方法必须指定完整的数据表名
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $params = $this->preExcludeFields($params);

        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
            $params[$this->dataLimitField] = $this->auth->id;
        }
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                $this->model->validateFailException()->validate($validate);
            }

$arr=[];

            if($params['dkjs']==''){
//                halt($params);
                $jidlog=Db::table('fa_jdklog')
                    ->where('username',$params['username'])
                    ->where('jdkid',$params['jdkid'])
                    ->where('zfip',$params['zfip'])
                    ->where('dkks',$params['dkks'])
                    ->find();
                if($jidlog){
                    $this->error(__('端口和ip已经添加', ''));
                }
                $result = $this->model->allowField(true)->save($params);
            }else{

                if ($params['dkjs']<$params['dkks']) {
                    $this->error(__('端口结束必须大于端口开始', ''));
                }

                for($i=$params['dkks'];$i<=$params['dkjs'];$i++)
                {
                    $jidlogdel=Db::table('fa_jdklog')
                        ->where('username',$params['username'])
                        ->where('jdkid',$params['jdkid'])
                        ->where('zfip',$params['zfip'])
                        ->where('dkks',$i)
                        ->find();
//                    halt($jidlogdel['id']);
                    if($jidlogdel){
                        Db::table('fa_jdklog')->delete($jidlogdel['id']);
                    }

//                    halt($jid);
                    $arr[]=[

//                        'ids'=>$jid['ids'],
                        'dkks'=>$i,
                        'zfip'=>$params['zfip'],
                        'jid'=>$jid['jid'],
                        'jdkid'=>$jid['id'],
                        'name'=>$jid['name'],
                        'username'=>$params['username'],

                    ];

                }


//                halt($arr);
                $result = $this->model->allowField(true)->saveAll($arr);

            }
    
//            $array = []; // 初始化一个空数组
//            for ($i = 1; $i <= 10; $i++) {
//                $array[] = $i; // 将循环中的值添加到数组中
//            }
//            halt($array);
//          halt($ks);

            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success();
    }







}
